<template>
  <div class="container">
    <div class="articles-container">
      <el-card class="box-card">
        <!-- 搜索 -->
         <el-form class="searchBox">
            <el-form-item label="关键字" class="selectBox1">
              <el-input
                v-model="query.keyword"
                class="filter-item"
                style="width: 200px;"
                placeholder="请输入搜索内容"
                size="small"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态" class="selectBox">
              <el-select v-model="query.state" placeholder="请输入状态内容" size="small">
                <el-option label="已启用" value="1"></el-option>
                <el-option label="已禁用" value="0"></el-option>
              </el-select>
              <el-button
                size="small"
                class="filter-item"
                style="margin-left: 10px;"
                @click="clearForm"
                >清除</el-button
              >
              <el-button
                size="small"
                type="primary"
                icon="el-icon-search"
                class="filter-item"
                @click="queryList"
                >搜索</el-button
              >
            </el-form-item>
            <el-form-item>
              <el-button
                class="filter-item fr"
                size="small"
                style="margin-left: 10px"
                type="success"
                icon="el-icon-edit"
                @click="showAddDialog"
                >新增技巧</el-button
              >
            </el-form-item>

        </el-form>

        <!-- 数据显示 -->
        <el-alert
          :title="'数据一共' + total + '条'"
          type="info"
          class="alert"
          show-icon
          closable
        ></el-alert>

        <!-- 表格 -->
        <el-table :data="artlist" stripe style="width: 100%" >
          <el-table-column type="index" label="序号" width="80" >
          </el-table-column>
          <el-table-column label="文章标题" width="400">
            <template #default="{ row }">
              {{ row.title }} &nbsp;
              <el-button
                type="text"
                v-if="row.videoURL"
                class="el-icon-film"
                style="color: blue"
                @click="videoShow(row.videoURL)"
              ></el-button>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数" width="80" >
          </el-table-column>
          <el-table-column prop="username" label="录入人"> </el-table-column>
          <el-table-column label="录入时间">
            <template #default="{ row }">
              {{ formatDate(row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template #default="{ row }">
              {{ formatState(row.state) }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="showView(row.id)">预览</el-button>
              <el-button type="text" @click="updateState(row.id, row.state)">
                {{ row.state ? "禁用" : "启用" }}
              </el-button>
              <el-button
                type="text"
                :disabled="row.state ? true : false"
                @click="showeditDialog(row.id)"
                >修改</el-button
              >
              <el-button
                type="text"
                :disabled="row.state ? true : false"
                @click="delArticle(row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="query.pagesize"
          layout=" prev, pager, next, sizes, jumper"
          :total="total"
          background
          style="margin: 20px; float: right"
        >
        </el-pagination>
      </el-card>
    </div>

    <!-- 新增 / 修改-->
    <ArticlesAdd
      :adddialog-visible="adddialogVisible"
      @closeDialog="closeDialog"
      @getList="getArtList"
      @updateOne="query.page = 1"
      ref="articleRef"
    />

    <!-- 预览 -->
    <ArticlePreView
      ref="preViewRef"
      :preViewdialogVisible="preViewdialogVisible"
      @closeView="closeView"
    />

    <!-- 视频 -->
    <div class="video" v-if="flag">
      <div class="video-close" @click="closeVideo()">
        <i class="el-icon-close"></i>
      </div>
      <div class="video-box">
        <video autoplay :src="url"></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '../../api/hmmm/articles'
import * as dayjs from 'dayjs'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlePreView from '../components/articles-preview.vue'
export default {
  components: { ArticlesAdd, ArticlePreView },
  data () {
    return {
      artlist: [],
      query: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      total: 0,
      // 新增对话框
      adddialogVisible: false,
      // 预览对话框
      preViewdialogVisible: false,
      flag: false,
      // 视频播放路径
      url: ''
    }
  },
  mounted () {
    this.getArtList()
  },
  methods: {
    async getArtList () {
      const res = await list(this.query)
      console.log(res)
      this.artlist = res.data.items
      this.total = res.data.counts
    },
    // 处理时间
    formatDate (val) {
      return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
    },
    // 处理状态
    formatState (state) {
      const map = {
        0: '已禁用',
        1: '已启用'
      }
      return map[state]
    },
    // 分页
    handleSizeChange (size) {
      this.query.pagesize = size
      this.query.page = 1
      this.getArtList()
    },
    handleCurrentChange (page) {
      this.query.page = page
      this.getArtList()
    },
    // 搜索
    async queryList () {
      this.query.page = 1
      this.getArtList(this.query)
    },
    // 清空
    clearForm () {
      this.query.keyword = ''
      this.query.state = null
      this.queryList()
    },
    // 修改状态
    async updateState (id, state) {
      state = state === 0 ? 1 : 0
      console.log(state)
      await changeState({
        id: id,
        state: state
      })
      this.getArtList()
      this.$message.success('操作成功')
    },
    // 视频播放
    videoShow (url) {
      console.log(url)
      this.url = url
      this.flag = true
    },
    // 关闭视频
    closeVideo () {
      this.flag = false
    },
    // 新增对话框
    showAddDialog () {
      this.adddialogVisible = true
    },
    // 关闭新增对话框
    closeDialog () {
      this.adddialogVisible = false
    },
    // 编辑和回显id
    showeditDialog (id) {
      this.$refs.articleRef.getArticleDetail(id)
      this.adddialogVisible = true
    },
    // 删除功能
    async delArticle (id) {
      this.$confirm('是否确认删除该文章?', '提示', {
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            await remove({ id })
            this.$message.success('删除成功')
            this.getArtList()
          }
        }
      })
    },
    // 预览对话框
    showView (id) {
      this.preViewdialogVisible = true
      this.$refs.preViewRef.getViewDetail(id)
    },
    closeView () {
      this.preViewdialogVisible = false
    }
  }
}
</script>

<style lang="less">
.el-card{
  margin: 10px;

  .el-alert{
    margin-bottom: 10px;
  }
//  .el-table td, .el-table th {
//     text-align: center;
//   }
}
.searchBox {
    display: flex;
    justify-content: space-between;
    height: 50px;
    margin-left: 18px;
    line-height: 45px;

    .selectBox{
      flex: 1.5;
    }
    .selectBox1{
      flex: 0.5;
    }
    .addBtn{
      margin-left: 20px;
    }
}

.video {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.4);
  overflow: hidden;

  .video-close {
    width: 50px;
    height: 50px;
    line-height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 25px;
    border-radius: 50%;
    text-align: center;
  }

  .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    video {
      width: 100%;
      height: 100%;
    }
  }
}
.el-icon-film{
  font-size: 18px;
}
</style>
